package com.ls.sort_algorithm;

import java.util.Arrays;
import java.util.Date;

public class InsertSort {

    int count = 0;

    public static void main(String[] args) {
        InsertSort insertSort = new InsertSort();

//        int[] arr = {3, 9, -1, -2, 10};

        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }

        System.out.println(new Date());
        insertSort.insertSort(arr);
        System.out.println(new Date());
        System.out.println(insertSort.count);
    }

    public void insertSort(int[] arr) {

        int insertVal = 0;//待插入值
        int insertIndex = 0;//待插入位置的索引
        for (int i = 1; i < arr.length; i++) {

            insertVal = arr[i];//待插入值
            insertIndex = i - 1;//待插入位置的索引

            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }

            arr[insertIndex + 1] = insertVal;

//            System.out.println(Arrays.toString(arr));
        }


    }
}
